package com.example.teamilkmall.controller;

import com.example.teamilkmall.controller.vo.AdminInfoVo;
import com.example.teamilkmall.controller.vo.ResultVO;
import com.example.teamilkmall.entity.AdminInfo;
import com.example.teamilkmall.service.AdminInfoService;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/adminInfo")
public class AdminInfoController extends BaseController {
    private final AdminInfoService adminInfoService;

    public AdminInfoController(AdminInfoService adminInfoService) {
        this.adminInfoService = adminInfoService;
    }
    @GetMapping("/listAdminInfo")
    public ResultVO<PageInfo<AdminInfo>> listAdmin(Integer limit, Integer page, String name){
        PageInfo<AdminInfo> pageInfo = adminInfoService.listAdmin(limit, page, name);
        return success(pageInfo);
    }

    @PostMapping("/updateAdminInfo")
    public ResultVO updateAdmin(AdminInfo admin){
        adminInfoService.updateAdmin(admin);
        return success();
    }

    @PostMapping("/deloneAdminInfo")
    public ResultVO deloneAdmin(Integer id){
        adminInfoService.deloneAdmin(id);
        return success();
    }

    @PostMapping("/addAdminInfo")
    public ResultVO addAdmin(AdminInfo admin){
        adminInfoService.addAdmin(admin);
        return success();
    }

    @PostMapping("/listAdminInfoAll")
    public ResultVO<List<AdminInfo>> getAdminList(){
        return success(adminInfoService.listAdminInfoAll());
    }

    @PostMapping("/login")
    public ResultVO<AdminInfo> login(@Valid AdminInfoVo adminInfoVo){
        AdminInfo adminInfo = new AdminInfo();
        BeanUtils.copyProperties(adminInfoVo,adminInfo);
        AdminInfo adminInfo1 = adminInfoService.auto(adminInfo);
        return success(adminInfo1);
    }
}
